package com.wqr.mobile.android.ui.pager;

import java.util.List;

import android.R.color;
import android.content.Context;
import android.os.AsyncTask.Status;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.wqr.mobile.android.ui.BaseActivity;
import com.wqr.mobile.android.ui.R;
import com.wqr.mobile.android.ui.api.API;
import com.wqr.mobile.android.ui.bean.ImageData;
import com.wqr.mobile.android.ui.bean.ImageData.ImageItem;
import com.wqr.mobile.android.ui.net.MyImagedownloader;
import com.wqr.mobile.android.ui.net.NetworkManager;
import com.wqr.mobile.android.ui.task.HttpRequestTask;
import com.wqr.mobile.android.ui.task.HttpRequestTask.HttpTaskCallBack;
import com.wqr.mobile.android.ui.task.HttpTaskResult;
import com.wqr.mobile.android.ui.utils.JsonUtil;

public class ImagePaper extends BaseActivity implements API, Callback, HttpTaskCallBack, OnPageChangeListener {
	private static final String TAG = ImagePaper.class.getSimpleName();
	private TextView topText;
	private ViewPager viewPager1;
	private List<ImageItem> images;
	private HttpRequestTask mImageTask;
	private NetworkManager mNetworkManager;
	private Context mContext;
	private View mLoadView;
	private Handler mHandler;
	private int mCurrentPager;

	private class ImageAdapter implements BaseViewAdapter {
		private MyImagedownloader mImagedownloader;

		public ImageAdapter(Context context) {
			this.mImagedownloader = new MyImagedownloader(context);
		}

		@Override
		public View getView(int position) {
			Log.i(TAG, "getView-->" + position);
			Log.i(TAG, "currentPager-->" + mCurrentPager);
			ImageView localImageView = new ImageView(mContext);
			localImageView.setBackgroundColor(color.black);
			mImagedownloader.loadBitmap(images.get(position).url, localImageView, mHandler);
			return localImageView;
		}
	}

	@Override
	public boolean handleMessage(Message msg) {
		switch (msg.what) {
		case START:
			mLoadView.setVisibility(VISIBLE);
			break;
		case FINISHED:
			mLoadView.setVisibility(GONE);
			break;

		default:
			break;
		}
		return false;
	}

	@Override
	public void onTaskStart() {
		mHandler.sendEmptyMessage(START);
	}

	@Override
	public HttpTaskResult onTaskRunning(Object... params) {
		String message = (String) params[0];
		if (mNetworkManager == null) {
			mNetworkManager = NetworkManager.getInstance();
		}
		return new HttpTaskResult(message, mNetworkManager.getRequest(mContext, "", message));
	}

	@Override
	public void onCancelled(Status status) {

	}

	@Override
	public void onTaskFinished(HttpTaskResult result) {
		if (result != null && result.getResult() != null) {
			if (result.getMessage().equals(IMAGE_HOST)) {
				ImageData data = JsonUtil.TransformJson2Vo(result.getResult(), ImageData.class);
				if (data != null) {
					images = data.images;
					ImgPagerAdapter localImgPagerAdapter = new ImgPagerAdapter(images.size(), new ImageAdapter(mContext));
					viewPager1.setAdapter(localImgPagerAdapter);
					viewPager1.setOnPageChangeListener(this);
					setTopText(1);
				}
				mHandler.sendEmptyMessage(FINISHED);
			}
		}

	}

	@Override
	protected void setUpViews() {
		setContentView(R.layout.activity_image_pager);
		topText = (TextView) findViewById(R.id.topText);
		viewPager1 = (ViewPager) findViewById(R.id.pager);
		mLoadView = findViewById(R.id.layout_load);
	}

	@Override
	protected void setUpModels() {
		mContext = this;
		mHandler = new Handler(this);
		mImageTask = new HttpRequestTask(this);
		mImageTask.execute(IMAGE_HOST);
	}

	@Override
	protected void destory() {
		if (mImageTask != null) {
			mImageTask = null;
		}
	}

	@Override
	public void onPageScrollStateChanged(int state) {
		// Log.i(TAG, "onPageScrollStateChanged-->" + state);
	}

	@Override
	public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
		// Log.i(TAG, "onPageScrolled-->" + "position=" + position);
	}

	@Override
	public void onPageSelected(int position) {
		setTopText(position + 1);
		mCurrentPager = position;
		Log.i(TAG, "onPageSelected-->" + position);
	}

	private void setTopText(int n) {
		String str1 = String.valueOf(String.valueOf(n));
		StringBuilder localStringBuilder = new StringBuilder(str1).append("/");
		String str2 = String.valueOf(images.size());
		String str3 = localStringBuilder + str2;
		topText.setText(str3);
	}
}